﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using Model.WaiGaoQiao;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ArticleRuleController : Controller
    {
        //
        // GET: /ArticleRule/
        Work_ArticleRuleInfo work = new Work_ArticleRuleInfo(true);
        public ActionResult ArticleRuleIndex()
        {
            return View();
        }
        /// <summary>
        /// 转到添加界面或者修改界面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddRule(int? id)
        {
            RuleVM idvm = new RuleVM();
           
            if (!id.HasValue)
            {
                idvm.ID = 0;
                idvm.ArticleName ="";
                idvm.Month =0;
                idvm.Remark = "";
             
            }
            else
            {

                Work_ArticleRuleInfo idinfo = work.GetAllArticleRule(Convert.ToInt32(id));
                Mapper.DynamicMap(idinfo, idvm);
                
            }
            return View(idvm);
        }
        /// <summary>
        /// 获取物品领用规则列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetArticleRuleList(PageVM pvm)
        {
           
            string condition =string.Empty;
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = work.GetWork_ArticleRuleCount(condition);
            IList<Work_ArticleRuleInfo> list = work.GetAllArticleRule(condition, pvm.rp, pvm.page).OrderByDescending(m=>m.Month).ToList();
            List<string> v;
            foreach (Work_ArticleRuleInfo ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.ArticleName);
                v.Add(ida.Month.ToString());
                v.Add(ida.Remark.ToString());
               
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 删除物品领用规则
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(int id)
        {
        return Json(work.DeleteArticleRule(id));
        }
        /// <summary>
        /// 保存物品领用规则
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveWorkArticleRule(RuleVM vm)
        {
            if (ModelState.IsValid)
            {
                Work_ArticleRuleInfo info = new Work_ArticleRuleInfo();
                Mapper.DynamicMap(vm, info);
                work.SaveWork_ArticleRule(info);
                return Json(true);
            }
            else
            {
                return Json(false);
            }


        }


    }
}
